/**
 * 
 */
package com.efast.dmom.mes.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.PlanOrder;
import com.efast.dmom.mes.bean.WorkOrder;
import com.efast.dmom.mes.bean.WorkOrderItem;
import com.efast.dmom.mes.bean.base.IModificationFlags;
import com.efast.dmom.mes.dao.entity.WorkOrderDao;
import com.efast.dmom.mes.entity.PlanOrderEntity;
import com.efast.dmom.mes.entity.WorkOrderEntity;
import com.efast.dmom.mes.entity.WorkOrderItemEntity;
import com.efast.dmom.mes.entity.base.ITraceObject;
import com.efast.dmom.mes.util.CommonConstants;

/**
 * @author Aaron Liu
 *
 *         2020年3月30日
 */
@Component
public class WorkOrderDataHandler {

	@Autowired
	private WorkOrderDao workOrderDao = null;
	
	@Autowired
	private LotDataHandler lotDataHandler = null;
	
	@Autowired
	private WorkOrderItemDataHandler orderItemDataHandler = null;
	
	

	public List<WorkOrder> saveWorkOrder(List<PlanOrder> planOrderList) {

		List<WorkOrder> orderList = new ArrayList<WorkOrder>();

		if (planOrderList == null || planOrderList.size() == 0) {
			return orderList;
		}
		
		List<WorkOrderItem> orderItemList = new ArrayList<WorkOrderItem>();

		for (PlanOrder planOrder : planOrderList) {
			
			if(planOrder.getModificationFlag() == IModificationFlags.NEW){
				
				PlanOrderEntity planOrderEntity = planOrder.getInnerData();

				WorkOrder workOrder = new WorkOrder();
				WorkOrderEntity workOrderEntity = new WorkOrderEntity();
				workOrderEntity.setCloseType(CommonConstants.CLOSE_TYPE_BUILD_TO_ORDER);
				workOrderEntity.setOrderNumber(planOrderEntity.getOrderNumber());
				workOrderEntity.setPlantId(planOrderEntity.getPlantId());
				workOrderEntity.setOrderStatus(CommonConstants.WORK_ORDER_STATUS_CREATED);
				workOrder.setInnerData(workOrderEntity);

				// one by one
				workOrderDao.save(workOrderEntity);

				WorkOrderItem orderItem = new WorkOrderItem();
				orderItem.setModificationFlag(IModificationFlags.NEW);
				
				WorkOrderItemEntity orderItemEntity = new WorkOrderItemEntity();
				// orderItemEntity.setBomName(bomName);
				// orderItemEntity.setBomRevison(bomRevision);
				orderItemEntity.setPartNumber(planOrderEntity.getPartNo());
				orderItemEntity.setOrderKey(workOrderEntity.getId());
				orderItemEntity.setPartRevision(planOrderEntity.getPartRevision());
				orderItemEntity.setQuantityOrdered(new BigDecimal(planOrderEntity.getQuantity()));
				orderItemEntity.setOrderItemStatus(CommonConstants.WORK_ORDER_ITEM_STATUS_CREATED);
				
				orderItem.setInnerData(orderItemEntity);
				orderItemList.add(orderItem);

				orderList.add(workOrder);
			}
		}
		
		if(orderItemList.size() > 0){
			orderItemDataHandler.saveBatch(orderItemList);
		}

		return orderList;
	}
	
	public void saveWorkOrder(WorkOrder workOrder){
		
		if (workOrder == null) {
			return;
		}

		WorkOrderEntity orderEntity = workOrder.getInnerData();

		if (orderEntity == null) {
			return;
		}
		
		if(workOrder.getModificationFlag() == IModificationFlags.UNCHANGED){
			return;
		}
		
		if(workOrder.getModificationFlag() == IModificationFlags.NEW){
		
			// ???
			if (orderEntity.getId() <= 0) {
				workOrderDao.save(orderEntity);
			}

			List<WorkOrderItem> itemList = workOrder.getOrderItemList();
			
			if(itemList != null && itemList.size() >0){
				
				for (WorkOrderItem orderItem : itemList) {
					
					WorkOrderItemEntity innderData = orderItem.getInnerData();
					innderData.setOrderKey(orderEntity.getId());
					innderData.setOrderItemStatus(CommonConstants.WORK_ORDER_ITEM_STATUS_CREATED);
					
				}
				orderItemDataHandler.saveBatch(itemList);
			}
		}
	}
	
	public void scheduleOrder(List<WorkOrder> orderList) {

		if (orderList == null) {
			return;
		}

		List<WorkOrderEntity> scheduleList = new ArrayList<WorkOrderEntity>();

		for (WorkOrder order : orderList) {

			WorkOrderEntity orderEntity = order.getInnerData();

			if (orderEntity.getOrderStatus() <= CommonConstants.WORK_ORDER_STATUS_AUDIT) {
				orderEntity.setOrderStatus(CommonConstants.WORK_ORDER_STATUS_SCHEDULED);
				scheduleList.add(orderEntity);
			}
		}

		if (scheduleList.size() > 0) {
			workOrderDao.scheduleOrders(scheduleList);
		}
	}
	
	public void broadcastOrder(List<WorkOrder> orderList, boolean toGenerate){
		
		if (orderList == null) {
			return;
		}

		List<WorkOrderEntity> broadcastOrderList = new ArrayList<WorkOrderEntity>();
		List<WorkOrderItemEntity> broadcastOrderItemList = new ArrayList<WorkOrderItemEntity>();
		
		for (WorkOrder order : orderList) {

			WorkOrderEntity orderEntity = order.getInnerData();
			if (orderEntity.getOrderStatus() < CommonConstants.WORK_ORDER_STATUS_FREEZED) {
				orderEntity.setOrderStatus(CommonConstants.WORK_ORDER_STATUS_BROADCAST);
				broadcastOrderList.add(orderEntity);
			}
			
			List<WorkOrderItem> orderItemList = order.getOrderItemList();
			for (WorkOrderItem orderItem : orderItemList) {

				WorkOrderItemEntity orderItemEntity = orderItem.getInnerData();
				orderItemEntity.setOrderItemStatus(CommonConstants.WORK_ORDER_STATUS_BROADCAST);
				
				if (toGenerate) {
					lotDataHandler.createLot(orderItem, orderItemEntity.getRouteId(), ITraceObject.LOT_TYPE_NORMAL);
				}
				broadcastOrderItemList.add(orderItemEntity);
			}
		}
		workOrderDao.broadcastOrders(broadcastOrderList);
		workOrderDao.broadcastOrderItems(broadcastOrderItemList);
	}

	public void closeOrder(long workOrderKey, String reason, String comments) {
		
	}

	public void finishOrder(long workOrderKey, String reason, String comments) {

	}

	public void holdOrder(long workOrderKey, String reason, String comments) {

	}

	public void removeOrder(long workOrderKey, String reason, String comments) {

	}

	public void undoClose(long workOrderKey, String reason, String comments) {

	}
}
